package day08;

public class Demo01 {
    public static void main(String[] args) {
        /*
         * 唯一的女朋友
         */
        //Girl girl1 = new Girl();
        //Girl girl2 = new Girl();
        Girl girl1 = Girl.getInstance();
        Girl girl2 = Girl.getInstance();

        Boy boy = Boy.getInstance();
    }
}

/**
 * 饿汉式
 */
class Girl{
    private String name = "沉鱼";
    private final static Girl instance = new Girl();
    private Girl(){
    }
    //访问点
    public static Girl getInstance() {
        return instance;
    }
}

/**
 * 懒汉式
 */
class Boy{
    private static Boy instance;
    private Boy(){
    }
    public static Boy getInstance() {
        //双检查方式
        if(instance==null){
            synchronized (Boy.class) {
                if (instance == null) {
                    instance = new Boy();
                }
            }
        }
        return instance;
    }
}

